package cn.zhixiangsingle.impl.warehouse;

import cn.zhixiangsingle.config.DynamicDataSourceContextHolder;
import cn.zhixiangsingle.dao.site.SiteMapper;
import cn.zhixiangsingle.dao.warehouse.IngredientWhouseMapper;
import cn.zhixiangsingle.date.DateUtils;
import cn.zhixiangsingle.entity.base.IsEmptyUtils;
import cn.zhixiangsingle.entity.base.site.SiteData;
import cn.zhixiangsingle.entity.base.site.vo.SiteVO;
import cn.zhixiangsingle.entity.warehouse.dto.IngredientWhouseDTO;
import cn.zhixiangsingle.entity.warehouse.vo.IngredientWhouseVO;
import cn.zhixiangsingle.pagination.MyStartEndUtil;
import cn.zhixiangsingle.service.warehouse.IngredientWhouseService;
import cn.zhixiangsingle.web.responsive.IStatusMessage;
import cn.zhixiangsingle.web.responsive.ResultBean;
import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.*;

/**
 * All rights Reserved, Designed By www.zhixiangyun.net
 *
 * @version V1.0
 * @Title: zhixiangpingtai
 * @Package cn.zhixiangsingle.impl.warehouse
 * @Description: ${todo}
 * @author: hhp
 * @date: 2019/10/5 11:20
 * @Copyright: 2019 www.zhixiangyun.net Inc. All rights reserved.
 * 注意：本内容仅限于浙江智飨科技内部传阅，禁止外泄以及用于其他的商业目
 */
@Service(version = "1.0.0", interfaceClass = IngredientWhouseService.class)
public class IngredientWhouseServiceImpl implements IngredientWhouseService {
    private static final Logger logger = LoggerFactory
            .getLogger(IngredientWhouseServiceImpl.class);
    @Autowired
    private SiteMapper siteMapper;
    @Autowired
    private IngredientWhouseMapper ingredientWhouseMapper;
    /**
     *@描述
     *@参数 [ingredientWhouseDTO]
     *@返回值 cn.zhixiangsingle.web.responsive.ResultBean
     *@创建人 hhp
     *@创建时间 2019/11/11
     *@修改人和其它信息
     */
    @Override
    public ResultBean findIngredientWhouseList(IngredientWhouseDTO ingredientWhouseDTO) throws Exception {
        ResultBean resultBean = new ResultBean();
        if(IsEmptyUtils.isEmpty(ingredientWhouseDTO.getPage())){
            ingredientWhouseDTO.setPage(1);
        }
        if (IsEmptyUtils.isEmpty(ingredientWhouseDTO.getLimit())){
            ingredientWhouseDTO.setLimit(10);
        }
        Integer page = ingredientWhouseDTO.getPage();
        Integer limit = ingredientWhouseDTO.getLimit();
        StringBuffer stringBufferJson = new StringBuffer();
        InputStream stream = getClass().getClassLoader().getResourceAsStream("json/siteData.json");
        BufferedReader br = new BufferedReader(new InputStreamReader(stream, "UTF-8"));
        String line;
        while ((line = br.readLine()) != null) {
            stringBufferJson.append(line);
        }
        JSONObject jobj = JSON.parseObject(stringBufferJson.toString());

        List<Map<String,Object>> mainLPLs = Lists.newArrayList();
        Integer total = 0;
        int startNum = (page-1)*limit;
        int endNum = startNum+limit;
        ArrayList<Integer> totalAll = Lists.newArrayList();
        ingredientWhouseDTO.setId(-1);
        if(IsEmptyUtils.isEmpty(ingredientWhouseDTO.getWhName())){
            ingredientWhouseDTO.setWhName("w");
        }
        if(IsEmptyUtils.isEmpty(ingredientWhouseDTO.getWhStatus())){
            ingredientWhouseDTO.setWhStatus("1");
        }
        if(!IsEmptyUtils.isEmpty(ingredientWhouseDTO.getSdIds())){
            String[] sdIds = ingredientWhouseDTO.getSdIds().split(",");
            StringBuffer stringBuffer = new StringBuffer();
            for(String sdId:sdIds){
                ingredientWhouseDTO.setSdId(Integer.parseInt(sdId));
                String dataSourceName = "";
                SiteData siteData = null;
                if(!IsEmptyUtils.isEmpty(jobj.get(sdId))){
                    siteData = JSON.parseObject(jobj.get(sdId).toString(),SiteData.class);
                }
                ingredientWhouseDTO.setPicturePrefix(siteData.getPicturePrefix());
                if(!IsEmptyUtils.isEmpty(siteData)){
                    dataSourceName = siteData.getDateSourceName();
                }
                if(!IsEmptyUtils.isEmpty(dataSourceName)){
                    if(IsEmptyUtils.isEmpty(stringBuffer)){
                        stringBuffer.append(sdId);
                    }else{
                        stringBuffer.append(","+sdId);
                    }
                    logger.info("--"+dataSourceName+"--");
                    DynamicDataSourceContextHolder.setDataSourceType(dataSourceName);
                    Integer totalStatus = ingredientWhouseMapper.findIngredientWhouseTotal(ingredientWhouseDTO);
                    total = total + totalStatus;
                    totalAll.add(totalStatus);
                }
            }
            ArrayList<ArrayList<Integer>> startEndNums = MyStartEndUtil.getStartEndArray(totalAll,startNum,endNum);
            String[] sourceNames = stringBuffer.toString().split(",");
            for(int i=0;i<startEndNums.size();i++){
                ingredientWhouseDTO.setSdId(Integer.parseInt(sourceNames[i]));
                SiteData siteData = JSON.parseObject(jobj.get(sourceNames[i]).toString(),SiteData.class);
                ingredientWhouseDTO.setPicturePrefix(siteData.getPicturePrefix());
                logger.info("--"+siteData.getDateSourceName()+"--");
                DynamicDataSourceContextHolder.setDataSourceType(siteData.getDateSourceName());
                ingredientWhouseDTO.setStart(startEndNums.get(i).get(0));
                ingredientWhouseDTO.setEnd(startEndNums.get(i).get(1)-startEndNums.get(i).get(0));
                ingredientWhouseDTO.setSdId(Integer.parseInt(sourceNames[i]));
                ingredientWhouseDTO.setSiteName(siteData.getName());
                ingredientWhouseDTO.setSitePhoto(siteData.getPhoto());

                List<Map<String,Object>> mainLPList = ingredientWhouseMapper.findIngredientWhouseList(ingredientWhouseDTO);
                mainLPLs.addAll(mainLPList);
            }
            DynamicDataSourceContextHolder.setDataSourceType(null);
        }else{
            List<SiteVO> userSites = siteMapper.findUserSites(ingredientWhouseDTO.getUserId());
            StringBuffer stringBuffer = new StringBuffer();
            for(SiteVO siteVO:userSites){
                ingredientWhouseDTO.setSdId(siteVO.getSdId());
                String dataSourceName = "";
                String sdIdStr = siteVO.getSdId().toString();
                SiteData siteData = null;
                if(!IsEmptyUtils.isEmpty(jobj.get(sdIdStr))){
                    siteData = JSON.parseObject(jobj.get(sdIdStr).toString(),SiteData.class);
                }
                ingredientWhouseDTO.setPicturePrefix(siteData.getPicturePrefix());
                if(!IsEmptyUtils.isEmpty(siteData)){
                    dataSourceName = siteData.getDateSourceName();
                }
                if(!IsEmptyUtils.isEmpty(dataSourceName)){
                    if(IsEmptyUtils.isEmpty(stringBuffer)){
                        stringBuffer.append(siteVO.getSdId());
                    }else{
                        stringBuffer.append(","+siteVO.getSdId());
                    }
                    logger.info("null sdIds...--"+dataSourceName+"----");
                    DynamicDataSourceContextHolder.setDataSourceType(dataSourceName);
                    Integer totalStatus = ingredientWhouseMapper.findIngredientWhouseTotal(ingredientWhouseDTO);
                    total = total + totalStatus;
                    totalAll.add(totalStatus);
                }
            }
            ArrayList<ArrayList<Integer>> startEndNums = MyStartEndUtil.getStartEndArray(totalAll,startNum,endNum);
            String[] sourceNames = stringBuffer.toString().split(",");
            for(int i=0;i<startEndNums.size();i++){
                SiteData siteData = JSON.parseObject(jobj.get(sourceNames[i]).toString(),SiteData.class);
                logger.info("--"+siteData.getDateSourceName()+"--");
                ingredientWhouseDTO.setPicturePrefix(siteData.getPicturePrefix());
                ingredientWhouseDTO.setSdId(Integer.parseInt(sourceNames[i]));
                DynamicDataSourceContextHolder.setDataSourceType(siteData.getDateSourceName());
                ingredientWhouseDTO.setStart(startEndNums.get(i).get(0));
                ingredientWhouseDTO.setEnd(startEndNums.get(i).get(1)-startEndNums.get(i).get(0));
                ingredientWhouseDTO.setSdId(Integer.parseInt(sourceNames[i]));
                ingredientWhouseDTO.setSiteName(siteData.getName());
                ingredientWhouseDTO.setSitePhoto(siteData.getPhoto());

                List<Map<String,Object>> mainLPList = ingredientWhouseMapper.findIngredientWhouseList(ingredientWhouseDTO);
                mainLPLs.addAll(mainLPList);
            }
            DynamicDataSourceContextHolder.setDataSourceType(null);
        }
        resultBean.setSuccess(true);
        resultBean.setResult(mainLPLs);
        resultBean.setTotal(total);
        resultBean.setResultCode(IStatusMessage.SystemStatus.SUCCESS.getCode());
        resultBean.setMsg(IStatusMessage.SystemStatus.SUCCESS.getMessage());
        return resultBean;
    }
    /**
     * @version V1.0
     * @Title: zhixiangpingtai
     * @Description:
     * @author: hhp
     * @param:  * @param ingredientWhouseDTO
     * @date: 2019/10/14 19:08
     */
    @Override
    public ResultBean findAll(IngredientWhouseDTO ingredientWhouseDTO) throws Exception {
        ResultBean resultBean = new ResultBean();
        StringBuffer stringBufferJson = new StringBuffer();
        InputStream stream = getClass().getClassLoader().getResourceAsStream("json/siteData.json");
        BufferedReader br = new BufferedReader(new InputStreamReader(stream, "UTF-8"));
        String line;
        while ((line = br.readLine()) != null) {
            stringBufferJson.append(line);
        }
        JSONObject jobj = JSON.parseObject(stringBufferJson.toString());

        List<Map<String,Object>> mainLPLs = Lists.newArrayList();

        ArrayList<Integer> totalAll = Lists.newArrayList();
        ingredientWhouseDTO.setId(-1);
        if(IsEmptyUtils.isEmpty(ingredientWhouseDTO.getWhName())){
            ingredientWhouseDTO.setWhName("w");
        }
        if(IsEmptyUtils.isEmpty(ingredientWhouseDTO.getWhStatus())){
            ingredientWhouseDTO.setWhStatus("1");
        }

        if(!IsEmptyUtils.isEmpty(ingredientWhouseDTO.getSdIds())){
            String[] sdIds = ingredientWhouseDTO.getSdIds().split(",");
            for(String sdId:sdIds){
                ingredientWhouseDTO.setSdId(Integer.parseInt(sdId));
                String dataSourceName = "";
                SiteData siteData = null;
                if(!IsEmptyUtils.isEmpty(jobj.get(sdId))){
                    siteData = JSON.parseObject(jobj.get(sdId).toString(),SiteData.class);
                }
                ingredientWhouseDTO.setPicturePrefix(siteData.getPicturePrefix());
                if(!IsEmptyUtils.isEmpty(siteData)){
                    dataSourceName = siteData.getDateSourceName();
                }
                if(!IsEmptyUtils.isEmpty(dataSourceName)){
                    logger.info("--"+dataSourceName+"--");
                    DynamicDataSourceContextHolder.setDataSourceType(dataSourceName);

                    ingredientWhouseDTO.setPicturePrefix(siteData.getPicturePrefix());
                    logger.info("--"+siteData.getDateSourceName()+"--");
                    DynamicDataSourceContextHolder.setDataSourceType(siteData.getDateSourceName());

                    ingredientWhouseDTO.setSiteName(siteData.getName());
                    ingredientWhouseDTO.setSitePhoto(siteData.getPhoto());

                    List<Map<String,Object>> mainLPList = ingredientWhouseMapper.findIngredientWhouseList(ingredientWhouseDTO);
                    mainLPLs.addAll(mainLPList);

                }
            }
            DynamicDataSourceContextHolder.setDataSourceType(null);
        }else{
            List<SiteVO> userSites = siteMapper.findUserSites(ingredientWhouseDTO.getUserId());
            StringBuffer stringBuffer = new StringBuffer();
            for(SiteVO siteVO:userSites){
                String dataSourceName = "";
                String sdIdStr = siteVO.getSdId().toString();
                SiteData siteData = null;
                if(!IsEmptyUtils.isEmpty(jobj.get(sdIdStr))){
                    siteData = JSON.parseObject(jobj.get(sdIdStr).toString(),SiteData.class);
                }
                ingredientWhouseDTO.setSdId(siteVO.getSdId());
                ingredientWhouseDTO.setPicturePrefix(siteData.getPicturePrefix());
                if(!IsEmptyUtils.isEmpty(siteData)){
                    dataSourceName = siteData.getDateSourceName();
                }
                if(!IsEmptyUtils.isEmpty(dataSourceName)){

                    logger.info("--"+siteData.getDateSourceName()+"--");
                    ingredientWhouseDTO.setPicturePrefix(siteData.getPicturePrefix());

                    DynamicDataSourceContextHolder.setDataSourceType(siteData.getDateSourceName());

                    ingredientWhouseDTO.setSiteName(siteData.getName());
                    ingredientWhouseDTO.setSitePhoto(siteData.getPhoto());

                    List<Map<String,Object>> mainLPList = ingredientWhouseMapper.findIngredientWhouseList(ingredientWhouseDTO);
                    mainLPLs.addAll(mainLPList);
                }
            }
            DynamicDataSourceContextHolder.setDataSourceType(null);
        }
        resultBean.setSuccess(true);
        resultBean.setResult(mainLPLs);
        resultBean.setResultCode(IStatusMessage.SystemStatus.SUCCESS.getCode());
        resultBean.setMsg(IStatusMessage.SystemStatus.SUCCESS.getMessage());
        return resultBean;
    }

    @Override
    public ResultBean updateIngredientWhouse(IngredientWhouseDTO ingredientWhouseDTO) throws Exception {
        return null;
    }

    @Override
    public ResultBean addIngredientWhouse(IngredientWhouseDTO ingredientWhouseDTO) throws Exception {
        return null;
    }
    /**
     *@描述
     *@参数 [ingredientWhouseDTO]
     *@返回值 java.util.List<cn.zhixiangsingle.web.responsive.ResultBean>
     *@创建人 hhp
     *@创建时间 2019/11/11
     *@修改人和其它信息
     */
    @Override
    public List<ResultBean> getIngredientWhouses(IngredientWhouseDTO ingredientWhouseDTO) throws Exception {
        StringBuffer stringBufferJson = new StringBuffer();
        InputStream stream = getClass().getClassLoader().getResourceAsStream("json/siteData.json");
        BufferedReader br = new BufferedReader(new InputStreamReader(stream, "UTF-8"));
        String line;
        while ((line = br.readLine()) != null) {
            stringBufferJson.append(line);
        }
        JSONObject jobj = JSON.parseObject(stringBufferJson.toString());

        List<ResultBean> mainLPLs = Lists.newArrayList();

        if(IsEmptyUtils.isEmpty(ingredientWhouseDTO.getWhName())){
            ingredientWhouseDTO.setWhName("w");
        }
        if(IsEmptyUtils.isEmpty(ingredientWhouseDTO.getWhStatus())){
            ingredientWhouseDTO.setWhStatus("1");
        }
        String exportTopTitle = "仓库基础资料信息-"+DateUtils.formatDate(new Date(),"yyyy-MM-dd")+"导出";
        if(!IsEmptyUtils.isEmpty(ingredientWhouseDTO.getSdIds())){
            String[] sdIds = ingredientWhouseDTO.getSdIds().split(",");
            for(String sdId:sdIds){
                ingredientWhouseDTO.setSdId(Integer.parseInt(sdId));
                String dataSourceName = "";
                SiteData siteData = null;
                if(!IsEmptyUtils.isEmpty(jobj.get(sdId))){
                    siteData = JSON.parseObject(jobj.get(sdId).toString(),SiteData.class);
                }
                if(!IsEmptyUtils.isEmpty(siteData)){
                    dataSourceName = siteData.getDateSourceName();
                }
                if(!IsEmptyUtils.isEmpty(dataSourceName)){
                    logger.info("--"+dataSourceName+"--");
                    DynamicDataSourceContextHolder.setDataSourceType(dataSourceName);

                    ingredientWhouseDTO.setSiteName(siteData.getName());

                    Map columnWidth = new HashMap();
                    columnWidth.put(0,6000);
                    columnWidth.put(1,4000);
                    List<List<String>> topTitles = Lists.newArrayList();
                    List<String> col0 = Lists.newArrayList();
                    List<String> col1 = Lists.newArrayList();
                    col0.add(exportTopTitle);col0.add("仓库名称");
                    col1.add(exportTopTitle);col1.add("仓库状态");
                    topTitles.add(col0);
                    topTitles.add(col1);
                    ResultBean resultBean = new ResultBean();
                    resultBean.setMsg(siteData.getName());
                    resultBean.setObj(columnWidth);
                    List<IngredientWhouseVO> mainLPList = ingredientWhouseMapper.findAllIngredientWhouses(ingredientWhouseDTO);
                    resultBean.setRows(topTitles);
                    resultBean.setResult(mainLPList);
                    mainLPLs.add(resultBean);

                }
            }
            DynamicDataSourceContextHolder.setDataSourceType(null);
        }else{
            List<SiteVO> userSites = siteMapper.findUserSites(ingredientWhouseDTO.getUserId());
            StringBuffer stringBuffer = new StringBuffer();
            for(SiteVO siteVO:userSites){
                ingredientWhouseDTO.setSdId(siteVO.getSdId());
                String dataSourceName = "";
                String sdIdStr = siteVO.getSdId().toString();
                SiteData siteData = null;
                if(!IsEmptyUtils.isEmpty(jobj.get(sdIdStr))){
                    siteData = JSON.parseObject(jobj.get(sdIdStr).toString(),SiteData.class);
                }

                if(!IsEmptyUtils.isEmpty(siteData)){
                    dataSourceName = siteData.getDateSourceName();
                }
                if(!IsEmptyUtils.isEmpty(dataSourceName)){

                    logger.info("--"+siteData.getDateSourceName()+"--");

                    DynamicDataSourceContextHolder.setDataSourceType(dataSourceName);

                    ingredientWhouseDTO.setSiteName(siteData.getName());

                    Map columnWidth = new HashMap();
                    columnWidth.put(0,6000);
                    columnWidth.put(1,4000);
                    List<List<String>> topTitles = Lists.newArrayList();
                    List<String> col0 = Lists.newArrayList();
                    List<String> col1 = Lists.newArrayList();
                    col0.add(exportTopTitle);col0.add("仓库名称");
                    col1.add(exportTopTitle);col1.add("仓库状态");
                    topTitles.add(col0);
                    topTitles.add(col1);
                    ResultBean resultBean = new ResultBean();
                    resultBean.setMsg(siteData.getName());
                    resultBean.setObj(columnWidth);
                    List<IngredientWhouseVO> mainLPList = ingredientWhouseMapper.findAllIngredientWhouses(ingredientWhouseDTO);
                    resultBean.setRows(topTitles);
                    resultBean.setResult(mainLPList);
                    mainLPLs.add(resultBean);
                }
            }
            DynamicDataSourceContextHolder.setDataSourceType(null);
        }
        return mainLPLs;
    }
}
